
##############
# POR LINHAS #
##############

import xlrd
import sqlite3
import os

if (os.path.exists('hippie-linhas-TESTE.csv')):
    os.unlink('hippie-linhas-TESTE.csv')

csvfile = open('hippie-linhas-TESTE.csv', 'w')

ESTAB_ENSINO = ''
UNI_ORGANICA = ''
NIVEL_FORMACAO = ''
CURSO_SUP = ''
AREA_EDU_FOR = ''

wb = xlrd.open_workbook("hippie-mini.xls")
for sheetname in wb.sheet_names() :
    sh = wb.sheet_by_name("Sheet1")
    
    for rownum in range(4, sh.nrows, 1):
        listbdd = []
        if sh.cell_value(rownum,0) == 'TOTAL':
            break
        else:
            if sh.cell_value(rownum,0) != '':
                ESTAB_ENSINO = sh.cell_value(rownum, 0).encode('latin-1')
                #print 'ENSINO : ', ESTAB_ENSINO
            if sh.cell_value(rownum, 1) != '':
                UNI_ORGANICA = sh.cell_value(rownum, 1).encode('latin-1')
                #print 'UNI : ', UNI_ORGANICA
            if sh.cell_value(rownum,2) != '':
                NIVEL_FORMACAO = sh.cell_value(rownum, 2).encode('latin-1')
                #print 'NIVEL : ', NIVEL_FORMACAO
            if sh.cell_value(rownum,3) != '':
                CURSO_SUP = sh.cell_value(rownum, 3).encode('latin-1')
                #print 'CURSO : ', CURSO_SUP
            if sh.cell_value(rownum,4) != '':
                AREA_EDU_FOR = sh.cell_value(rownum, 4).encode('latin-1')
                #print 'AREA : ', AREA_EDU_FOR
            
            for col in range(sh.ncols):         
                if sh.cell(rownum,col).value == "-":
                        listbdd.append(str("0.0"))
                elif isinstance(sh.cell(rownum,col).value, float):
                    listbdd.append(str(sh.cell(rownum,col).value))
                else:
                    if (sh.cell(rownum,col).value) == '' and col == 0:
                        listbdd.append(str(ESTAB_ENSINO)) # se coluna 0 e vazia
                    elif (sh.cell(rownum,col).value) == '' and col == 1:
                        listbdd.append(str(UNI_ORGANICA)) # se coluna 1 e vazia
                    elif (sh.cell(rownum,col).value) == '' and col == 2:
                        listbdd.append(str(NIVEL_FORMACAO)) # se coluna 2 e vazia
                    elif (sh.cell(rownum,col).value) == '' and col == 3:
                        listbdd.append(str(CURSO_SUP)) # se coluna 3 e vazia
                    elif (sh.cell(rownum,col).value) == '' and col == 4:
                        listbdd.append(str(AREA_EDU_FOR)) # se coluna 4 e vazia
                    else:
                        listbdd.append(str((sh.cell(rownum,col).value).encode('latin-1')))

            for element in listbdd:
                csvfile.write(element)

            csvfile.write('\n\n')
    print("-- LINHAS = %i  --" % rownum)
print("########## FIM ##########")
csvfile.close()

###############
# POR COLUNAS #
###############

'''
import xlrd

csvfile = open('hippie.csv', 'w')

wb = xlrd.open_workbook("hippie.xls")

for sheetname in wb.sheet_names() :
    sh = wb.sheet_by_name(sheetname)
    for rownum in range(sh.nrows) :
        print sh.row_values(rownum)
        values = []        
        for col in range(sh.ncols):
            if isinstance(sh.cell(rownum,col).value, float):
                listbdd.append(str(sh.cell(rownum,col).value))
            else:
                listbdd.append((sh.cell(rownum,col).value).encode('latin-1'))
    listbdd.append('\n')
csvfile.close()
'''
